package com.hbnu;

import com.hbnu.handler.ExceptionHandler;
import com.hbnu.handler.FileDeleteHandler;
import com.hbnu.handler.FileServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

public class Main {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        // LoggingHandler loggingHandler = new LoggingHandler();

        try {
            serverBootstrap.group(boss, worker);
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) {
                    //日志
                    //socketChannel.pipeline().addLast(loggingHandler);
                    socketChannel.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                    socketChannel.pipeline().addLast(new ObjectEncoder());
                    socketChannel.pipeline().addLast(new FileServerHandler());
                    socketChannel.pipeline().addLast(new FileDeleteHandler());
                    socketChannel.pipeline().addLast(new ExceptionHandler());


                }
            });
            //serverBootstrap.bind(8972);
            Channel channel = serverBootstrap.bind(8972).sync().channel();
            //添加日志
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            System.out.println("xlh");
            throw new RuntimeException(e);
        }finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}